<template>
	<view>
		<uni-popup ref="popup" type="center" border-radius="10px">
			<uni-card :title="title" thumbnail="" extra="额外信息" note="Tips" style="width: 100%;">
				<template v-slot:title>
					<view class="gl-header" style="border-bottom: 2px solid #aaa;">
						<view class="gl-header-left">
							<text>{{title}}</text>
						</view>
						<view class="gl-header-right">
							<uni-icons type="closeempty" size="16" color="#fff" class="gl-button-icon" @tap="eventClose"
								style="background-color: #dd524d;padding: 2px;border-radius: 2px;"></uni-icons>
						</view>
					</view>
				</template>

				<uni-data-select v-model="curValue" :localdata="couponData" :disabled="isFull" label="选择面额"
					:clear="false" placeholder="请选择面额"></uni-data-select>
				<view class="" style="display: flex;gap: 15px;align-items: center;padding: 5px 0;">
					<text style="font-weight: bold;">发卡数量:</text>
					<uni-number-box v-model="curCount" :min="1" :max="10" :step="1" :disabled="isFull"
						@change="numberChange"></uni-number-box>
				</view>
				<view class="" style="display: flex;gap: 15px;align-items: center;padding: 5px 0;">
					<text style="font-weight: bold;">发放套卡:</text>
					<switch :checked="isFull" color="#FFCC33" style="transform:scale(0.7)" @change="switchChange" />
				</view>
				<view class="" style="display: flex;gap: 15px;align-items: center;padding: 5px 0;">
					<text style="font-weight: bold;">注:</text>
					<text>套卡总金额为200元，面额：5元 * 10；10元 * 5；20元 * 2；30元 * 2</text>
				</view>

				<view slot="actions" class="card-actions"
					style="display: flex;justify-content: flex-end;gap: 15px;padding: 20px 0;">
					<view class="gl-button gl-button-base" @tap="eventSubmit">
						<uni-icons type="checkmarkempty" size="18" color="#fff"></uni-icons>
						<text style="font-size: small;">保存</text>
					</view>
					<view class="gl-button gl-button-warning" @tap="eventClose">
						<uni-icons type="closeempty" size="18" color="#fff"></uni-icons>
						<text style="font-size: small;">取消</text>
					</view>
				</view>
			</uni-card>
		</uni-popup>
	</view>
</template>

<script>
	import psMethod from '../../common/psMethod'

	export default {
		data() {
			return {
				title: '发放代金券',
				sConf: getApp().globalData,
				curSid: null,
				curValue: 500,
				curCount: 1,
				isFull: false,
				couponData: [{
					value: 500,
					text: '5元'
				}, {
					value: 1000,
					text: '10元'
				}, {
					value: 2000,
					text: '20元'
				}, {
					value: 3000,
					text: '30元'
				}]
			}
		},
		methods: {
			open(e) {
				this.curSid = e
				this.$refs.popup.open()
			},
			eventClose() {
				this.$refs.popup.close()
			},
			numberChange(e) {
				console.log('numberChange', e, this.curCount);
			},
			switchChange(e) {
				console.log('switchChange', e.detail.value);
				this.isFull = e.detail.value
			},
			eventSubmit() {
				console.log('eventSubmit');
				let that = this
				uni.showModal({
					title: '身份认证',
					placeholderText: '请验证二次密码',
					editable: true,
					success(r) {
						if (r.confirm && r.content === '253100') {
							that.loadAddCoupon().then(res => {
								console.log('发放代金券', res);
								that.$emit('refresh')
								that.eventClose()
							}).catch(err => {
								uni.showModal({
									title: '错误',
									content: '提交失败了',
									showCancel:false
								})
							})
						}
					}
				})
			},
			loadAddCoupon() {
				let host = this.sConf.host + '/admin/couponSend/';
				let token = this.sConf.auth.token || '';
				let data = {
					sid: this.curSid,
					status: this.isFull ? 1 : 0
				}
				if (!this.isFull) {
					data["coupon"] = this.curValue
					data["count"] = this.curCount
				}
				console.log('请求参数->', data);
				return new Promise((resolve, reject) => {
					psMethod.authenticatorPostSync(host, token, data).then(res => {
						resolve(res)
					}).catch(err => {
						reject(err)
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>

</style>